<?php
namespace XtwsBack\Controller;
use Think\Controller;
class WithDrawJFController extends CommonController {

    // 积分兑换金额提现
    public function index(){

        $member  = M('member');
        // 查询余额不为0的店铺老板信息
        if (IS_POST && !empty($_POST['storename'])){
            $u_id = M("member")->where("nickname LIKE '%".$_POST['storename']."%'")->getField('u_id');//店主id
            $map['id'] = array('eq',$u_id);
        }else{
            $map['credit3'] = array('neq','0.00');
        }//end if
        $map['status'] = array('eq','0');
        $count = $member->where($map)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25),查出一百条记录到前台，再由前台js处理成分页
        $show = $Page->show();// 分页显示输出
        $list = $member ->order('id desc')->where($map)->limit($Page->firstRow.','.$Page->listRows)->field('nickname,realname,credit3,id')->select();
        $this -> assign('list',$list);
        $this -> assign('page',$show);
        //加载视图
        $this->display();
    }//end fun

    public function loginfo(){
        $u_id = I('u_id');
//        $a_date = $_GET['date'];  //获取日期

        $member = M('member');
//        $shop_pay_log = M('shop_pay_log');
        $exchange_order = M("exchange_order");

//        $date = explode("-",$a_date);
//        $year = $date[0];
//        $month = $date[1];
//        $day = $date[2];
//        $s_time = mktime(0,0,0,$month,$day,$year);      //当天开始时间戳
//        $e_time= mktime(23,59,59,$month,$day,$year);    //当天结束时间戳

        //查询条件
        $store_c_id = M("shop_store")->where("u_id={$u_id} AND status = 1")->getField('c_id');//店铺小区id



//        dump($store);
//        dump($spellgroup);
//        dump($spellgroup_copy);
        $where['c_id']=array('eq',$store_c_id);
        $where['state'] = array('in',array(2,3));
        $where['canceltime']=array("gt",0);
//            $where['paytime'] = array('between',array($s_time,$e_time));

        //微信支付
        $wxpay_arr = $exchange_order->where($where)->select();   //收款明细
//        dump($where);


        $arr_id = array();
        $wxpay_list = array();
        foreach ($wxpay_arr as $k=>$v){
            $a_time = date("Y-m-d",$v['canceltime']);
            $wxpay_arr[$k]['is_tx'] = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 13 AND `clearingdate` LIKE '{$a_time}'" )->getField('id');
            if($wxpay_arr[$k]['is_tx']){
                array_push($arr_id,$v['id']);
            }//end if
        }//end foreach

        foreach ($wxpay_arr as $k=>$v){
            $a_time = date("Y-m-d",$v['canceltime']);
            $wxpay_list[strtotime($a_time)][]=$v;
        }//end foreach
        krsort($wxpay_list);

        foreach ($wxpay_list as $k=>$v){
            foreach ($v as $key=>$val){
                $wxpay_list[$k][$key]['fee']= ($val['total_fee']+$val['card_fee']);
                $wxpay_list[$k][$key]['sx_fee'] = round($wxpay_list[$k][$key]['fee']*0.003, 2)+round($wxpay_list[$k][$key]['fee']*0.007, 2)+round($wxpay_list[$k][$key]['fee']*0.006, 2);//手续费
                $wxpay_list[$k]['wxpay_total'] += $wxpay_list[$k][$key]['fee'];     //微信收款总金额

                $wxpay_list[$k]['wx_total_fee']  += $wxpay_list[$k][$key]['sx_fee']; //手续费求和

                $wxpay_list[$k][$key]['realname'] =M("member")->where("id = {$val['u_id']}")->getField('realname') ;

            }//end foreach
//                $wxpay_list[$k]['integral_fee'] = round($v['wxpay_total']*0.007, 2);          //积分费四舍五入
//                $wxpay_list[$k]['wx_service_fee'] = round($v['wxpay_total']*0.006, 2);          //积分费四舍五入
        }//end foreach


        if ($arr_id){
            $where['id']=array('not in',$arr_id);
        }//end if
        $wxpay_total = $exchange_order->where($where)->sum('total_fee')+$exchange_order->where($where)->sum('card_fee');     //微信收款总金额
        $wxpay_fee = round($wxpay_total*0.003, 2);          //积分费四舍五入
        $integral_fee = round($wxpay_total*0.007, 2);          //积分费四舍五入
        $service_fee = round($wxpay_total*0.006, 2);          //积分费四舍五入
        $data['wxpay_fee'] = $wxpay_fee;
        $data['integral_fee'] = $integral_fee;
        $data['service_fee'] = $service_fee;

        //微信可提现金额
        $data['wx_total_fee'] = round($wxpay_total-$wxpay_fee-$integral_fee-$service_fee,2);

        $m_info  = $member->where("id={$u_id}")->field('nickname,realname,credit3,id')->find();
//        dump($wxpay_arr);
//        dump($wxpay_list);
        $this -> assign('date',$a_date);
        $this -> assign('wxpay_list',$wxpay_list);
        $this -> assign('wxpay_total',$wxpay_total);
        $this -> assign('m_info',$m_info);
        $this -> assign($data);
        //加载视图
        $this->display();
    }// end fun

    // 提现-余额变动
    public function changeCredit3(){
        if(IS_POST){
            $post =  I('post.');
            $u_id = $post['u_id'];
            $date = $post['date'];

//            dump($date);die;
            // 查询用户是否已提现
            $is_tx = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 13 AND `clearingdate` LIKE '{$date}'" )->getField('id');
            if($is_tx>0){
                $this->ajaxReturn(array('status'=>1,'msg'=>'已提现'),"JSON");
            }else{
//                dump($date);die;
                // 用户余额减少
                $res = M('member')->where('id = '.$post['u_id'])->setDec('credit3',$post['credit3']);
                if($res){
//                        增加余额变动记录
                    $result = changeCredit($post['u_id'],'提现（积分兑换）','credit3','-',$post['credit3'],13,$date);
                    if($result){
                        $this->ajaxReturn(array('status'=>0,'msg'=>'提现成功'),"JSON");
                    }
                }//end if
            }//end if
        }else{
            $this->error('非法进入');
        }//end if
    }//end fun


}// end class